package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.entity.Airport;
import com.sz.biz.logistics.base.service.AirportService;
import com.sz.biz.logistics.base.dto.AirportDto;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: AirportController <br>
 * Author: penghui.zheng <br>
 * Date: 2016-11-23 10:19:00
 */
@RestController
@RequestMapping("/api/v1/biz/airport")
@Api(description = " ", tags = "027、机场管理")
public class AirportController extends BaseController {
    //Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private AirportService airportService;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.biz.airport}")
    private String i18nTable;


    /**
     * 分页查询机场
     *
     * @throws Exception
     */
    @ApiOperation(value = "搜索机场", notes = "搜索机场<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countryId", value = "国家必填项，这样才能区分到后台去查地区那张表", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "cityId", value = "城市", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/searchlist", method = {RequestMethod.GET})
    public
    @ResponseBody
    ResultDto findAirportList(@RequestParam(required = false) String countryId,
                              @RequestParam(required = false) String cityId, @RequestParam(required = false) String q,
                              @RequestParam int pageIndex, @RequestParam int pageSize, @RequestParam(required = false) String sort,
                              @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> map = new HashMap<>();
        map.put("countryId", countryId);
        map.put("cityId", cityId);
        ResultDto dto = airportService.findAirportList(new QueryParams(pageSize, pageIndex, q, sort, asc).put(map));
        return dto;
    }

    /**
     * 分页查询机场-短接口
     *
     * @throws Exception
     */
    @ApiOperation(value = "搜索机场-短接口", notes = "搜索机场<br>权限：")
    @ApiImplicitParams({
    	    @ApiImplicitParam(name = "countryId", value = "国家必填项，这样才能区分到后台去查地区那张表", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "cityId", value = "城市", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/shortlist", method = {RequestMethod.GET})
    public
    @ResponseBody
    ResultDto findShortlList(@RequestParam(required = false) String countryId,
                              @RequestParam(required = false) String cityId,
                              @RequestParam(required = false) String q,
                              @RequestParam int pageIndex, 
                              @RequestParam int pageSize, 
                              @RequestParam(required = false) String sort,
                              @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> map = new HashMap<>();
        map.put("countryId", countryId);
        map.put("cityId", cityId);
        return airportService.shortAirportListPage(new QueryParams(pageSize, pageIndex, q, sort, asc).put(map));
    }

    /**
     * 新建一个机场
     *
     * @throws Exception
     */
    @ApiOperation(value = "新建机场", notes = "新建机场<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({@ApiImplicitParam(name = "airportDto", value = "提交机场信息\r\n "
            + "airportName,triadCode,tetradCode", required = true, dataType = "AirportDto", paramType = "body")})
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public ResultDto saveAirport(@RequestBody AirportDto airportDto) {
        Airport airport = new Airport();
        airportDto.setId(null);// 保存时忽略FeeId
        EntityUtils.copyPropertiesIgnoreNull(airportDto, airport);
        int id = airportService.saveAirport(airportDto);
        i18nService.save(id, airportDto.getI18n(), i18nTable);
        ResultDto result = this.getSaveSuccessDto(MessagesCodes.MODULE_AIRPORT);
        result.setData(id);
        return result;
    }

    /**
     * 修改机场
     *
     * @throws Exception
     */
    @ApiOperation(value = "修改机场", notes = "修改机场<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改机场的id ", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "airportDto", value = "提交机场信息\r\n "
                    + "airportName,triadCode,tetradCode", required = true, dataType = "AirportDto", paramType = "body")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public
    @ResponseBody
    ResultDto updateAirport(@PathVariable int id, @RequestBody AirportDto airportDto) {
        airportDto.setId(id);
        airportService.updateAirport(airportDto);
        i18nService.save(id, airportDto.getI18n(), i18nTable);
        ResultDto dto = this.getUpdateSuccessDto(MessagesCodes.MODULE_AIRPORT);
        dto.setData(id);
        return dto;
    }

    /**
     * 根据airportId删除机场 ,逻辑删除
     *
     * @throws Exception
     */
    @ApiOperation(value = "删除机场", notes = "删除机场<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids", required = true, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public
    @ResponseBody
    ResultDto deleteAirport(@RequestBody List<Integer> ids) {
        airportService.deleteByAirportIds(ids);
        return getDeleteSuccessDto(MessagesCodes.MODULE_AIRPORT);
    }

    /**
     * 根据机场id查询机场
     *
     * @throws Exception
     */
    @ApiOperation(value = "查询机场", notes = "查询机场<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "机场的id ", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getAirportById(@PathVariable int id) {
        AirportDto airportDto = airportService.findByAirportId(id);
        ResultDto dto = new ResultDto();
        dto.setData(airportDto);
        return dto;
    }
    /**
     * 根据机场id查询机场短接口
     *
     * @throws Exception
     */
    @ApiOperation(value = "查询机场短接口", notes = "查询机场短接口<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "机场的id ", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "short/{id}", method = {RequestMethod.GET})
    public ResultDto findShortById(@PathVariable int id) {
        Airport airport = airportService.findShortById(id);
        ResultDto dto = new ResultDto();
        dto.setData(airport);
        return dto;
    }

    /**
     * 根据条件获取国际化数据集合
     *
     * @param language  语言信息
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param sort      排序字段的名称
     * @param asc       true asc false desc
     * @return 查询的国际化数据集合
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "查询i18n", notes = "根据指定条件获取国际化数据集合<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "path", required = true),
            @ApiImplicitParam(name = "unfilled", value = "未填写项", dataType = "Boolean", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query")}

    )
    @RequestMapping(value = "i18n/list/{language}", method = RequestMethod.GET)
    public ResultDto findI18n(@PathVariable String language, @RequestParam(required = false) String q,
                              @RequestParam(required = true) Boolean unfilled,
                              @RequestParam(required = false, defaultValue = "1") int pageIndex,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false) String sort,
                              @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> map = new HashMap<>();
        map.put("unfilled", unfilled);
        ResultDto dto = airportService.findI18nByParam(new QueryParams(pageSize, pageIndex, q, sort, asc, language).put(map));
        return dto;
    }

    /**
     * 批量新增或修改国际化.
     *
     * @param dtos 要保存或修改的国际化实体集合
     * @return 处理成功与否的返回信息
     */
    @ApiOperation(value = "新增或修改i18n", notes = "批量新增或修改机场国际化<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @RequestMapping(value = "i18n", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public ResultDto saveI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true) List<I18nDto> dtos) {
        i18nService.save(dtos, i18nTable);
        airportService.refreshCache();
        return getSaveI18nSuccessDto();
    }

    /**
     * 异步校验机场名称
     */
    @ApiOperation(value = "异步校验机场名称", notes = "异步校验机场名称<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "机场名称", required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checkname", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public
    @ResponseBody
    ResultDto airportNameCheck(@RequestParam(required = true) String name) {
        airportService.checkAirportName(name);
        return new ResultDto();
    }

    /**
     * 异步校验机场三字码
     */
    @ApiOperation(value = "异步校验机场三字码", notes = "异步校验机场三字码<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "triadCode", value = "机场三字码", required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checktriadcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public
    @ResponseBody
    ResultDto triadCodeCheck(@RequestParam(required = false, defaultValue = "0") int id, @RequestParam(required = true) String triadCode) {
        airportService.checkTriadCode(triadCode, id);
        return new ResultDto();
    }

    /**
     * 异步校验机场四字码
     */
    @ApiOperation(value = "异步校验机场四字码", notes = "异步校验机场四字码<br>权限：" + Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tetradCode", value = "机场四字码", required = true, dataType = "string", paramType = "query")})
    @RequestMapping(value = "/checktetradcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_STATION_AIRPORT)
    public
    @ResponseBody
    ResultDto tetradCodeCheck(@RequestParam(required = true) String tetradCode) {
        airportService.checkTetradCode(tetradCode);
        return new ResultDto();
    }
}
